package com.magus.project.yddemo.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.magus.framework.core.persistence.JpaSearchUtils;
import com.magus.framework.core.persistence.SearchFilter;
import com.magus.framework.core.repository.BaseRepository;
import com.magus.framework.core.service.BaseService;
import com.magus.project.yddemo.entity.IndicatorRole;
import com.magus.project.yddemo.repository.IndicatorRoleRepository;
import com.magus.sis.common.entity.ComUnit;
import com.magus.sis.common.service.ComUnitService;

@Service
@Transactional(readOnly=true)
public class IndicatorRoleService extends BaseService<IndicatorRole, String> {

	@Autowired
	private IndicatorRoleRepository indicatorRoleRepository;
	
	@Autowired
	private ComUnitService comUnitSerivce;
	
	@Override
	protected BaseRepository<IndicatorRole, String> getBaseRepository() {
		return indicatorRoleRepository;
	}
	

	/**
	 * 获取数据
	 */

	
	
	
	/**
	 * 获取所有电厂的机组信息
	 * @return
	 */
	public List<ComUnit> getAllUnit(){
		List<SearchFilter> searchFilters = new ArrayList<>();
		searchFilters.add(SearchFilter.equal("deleted", Boolean.FALSE));
		Specification<ComUnit> buildAndSpec = JpaSearchUtils.buildAndSpec(searchFilters);
		return comUnitSerivce.findAll(buildAndSpec);
	}

	/**
	 * 保存数据
	 */
	@Transactional
	@Override
	public List<IndicatorRole> save(Iterable<IndicatorRole> entities) {
		for (IndicatorRole indicatorRole : entities) {
			List<SearchFilter> searchFilters = new ArrayList<>();
			searchFilters.add(SearchFilter.equal("role",indicatorRole.getRole()));
			searchFilters.add(SearchFilter.equal("dimension",indicatorRole.getDimension()));
			Specification<IndicatorRole> buildAndSpec = JpaSearchUtils.buildAndSpec(searchFilters);
			List<IndicatorRole> findAll = indicatorRoleRepository.findAll(buildAndSpec);
			indicatorRoleRepository.delete(findAll);
		}
		
		return super.save(entities);
	}
	
}
